﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace PortableLog
{
    public class LogManage
    {
        private static LogManage instance = new LogManage();
        private bool verbose = false;
        private int delete = 5;
        private Dictionary<string, Logger> loggers;
        private Timer timer;
        private int eclipse = 30;
        private ulong size = 20;
        private uint count = 5;

        private LogManage()
        {
            loggers = new Dictionary<string, Logger>();
        }

        public static LogManage Instance()
        {
            if (instance == null)
                instance = new LogManage();
            return instance;
        }

        public Logger GetLogger(string moduleName)
        {
            Logger logger;
            if (loggers.ContainsKey(moduleName))
            {
                logger = loggers[moduleName];
            }
            else
            {
                logger = new Logger(instance, moduleName);
                loggers[moduleName] = logger;
            }
            return logger;
        }

        public bool VerboseLogEnable
        {
            get { return this.verbose; }
            set 
            {
                this.verbose = value;
                if (this.verbose)
                {
                    if (null != timer)
                    {
                        timer.Dispose();
                        timer = null;
                    }
                    timer = new Timer(VerboseLogTimeoutCallback, null, VerboseLogEclipse * 60 * 1000, 0);
                }
            }
        }

        private void VerboseLogTimeoutCallback(object state)
        {
            VerboseLogEnable = false;
            if (null != timer)
            {
                timer.Dispose();
                timer = null;
            }
        }

        public uint MaxLogFileCount
        {
            get { return this.count; }
            set { this.count = value; }
        }

        public int VerboseLogEclipse
        {
            get { return this.eclipse; }
            set { this.eclipse = value; VerboseLogEnable = true; }
        }

        public ulong MaxLogFileSize
        {
            get { return this.size; }
            set { this.size = value; }
        }
    }

    public enum LoggingLevel
    {
        Verbose,
        Information,
        Warning,
        Error,
        Critical
    }
}
